﻿using Common;
using Entity;
using Entity.Front_Desk;
using IHotelBLL.IEmployeeBLL;
using IHotelDAL.IFrondeskDAL;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBLL.FrontdeskBLL
{
    public class FrontDeskBLL : IFrontDeskBLL
    {
        IFrontDeskDAL _frontDeskDAL;
        public FrontDeskBLL(IFrontDeskDAL frontDeskDAL)
        {
            _frontDeskDAL = frontDeskDAL;
        }
        public FrontDesk Login(string employeeNo, string password, out string msg)
        {
            FrontDesk employee = _frontDeskDAL.GetEntities().Where(e => e.EmployeeNo == employeeNo && e.Password == MD5Helper.MD5Encrypt64(password)).FirstOrDefault();
            if (employee == null)
            {
                msg = "账号或密码有误";
                return null;
            }
            msg = "登录成功";
            return employee;
        }
        /// <summary>
        /// 创建员工
        /// </summary>
        /// <param name="employeeName"></param>
        /// <param name="passWord"></param>
        /// <param name="employeeNo"></param>
        /// <param name="IsLeader"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool CreatFrontDeskInfo(FrontDesk frontDesk, out string msg)
        {
            //通过员工编号获取信息
            FrontDesk frontDeskInfo = GetFrontDeskInfoEmployeeNo(frontDesk.EmployeeNo);
            if (frontDeskInfo != null)
            {
                msg = "员工编号已存在";
                return false;
            }
            //使用MD5 加密密码
            string MD5password = MD5Helper.MD5Encrypt64(frontDesk.Password);
            //创建用户对象
            frontDesk.Id = Guid.NewGuid().ToString();
            frontDesk.Password = MD5password;
            bool isSuccess = _frontDeskDAL.CreateEntity(frontDesk);
            //三目运算符
            msg = isSuccess ? "创建成功" : "创建失败";
            return isSuccess;
        }

        /// <summary>
        ///通过员工Id
        /// </summary>
        /// <param name="employeeNo"></param>
        /// <returns></returns>
        private FrontDesk GetFrontDeskInfoEmployeeNo(string employeeNo)
        {
            return _frontDeskDAL.GetEntities().Where(f => f.EmployeeNo == employeeNo).FirstOrDefault();
        }
        public FrontDesk GetFrontDeskById(string Id)
        {
            return _frontDeskDAL.GetEntities().Where(f => f.Id == Id).FirstOrDefault();
        }

        public bool UpdateFrontDeskInfo(FrontDesk frontDesk, out string msg)
        {
            if (GetFrontDeskById(frontDesk.Id) is null)
            {
                msg = "未找到员工";
                return false;
            }
            //修改数据
            frontDesk.Password = MD5Helper.MD5Encrypt64(frontDesk.Password);
            bool isSuccess = _frontDeskDAL.UpdateEntity(frontDesk);
            msg = !isSuccess ? "修改失败" : "修改成功";
            return isSuccess;
        }
        /// <summary>
        /// 删除员工
        /// </summary>
        /// <param name="id"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool DeleteFrontDeskInfo(string employeeNo, out string msg)
        {

            //获取员工信息
            FrontDesk frontDesk = _frontDeskDAL.GetEntities().FirstOrDefault(f => f.Id == employeeNo);
            if (frontDesk == null)
            {
                msg = "删除的员工不存在";
                return false;
            }
            bool isSuccess = _frontDeskDAL.UpdateEntity(frontDesk);
            msg = isSuccess ? "删除成功" : "删除失败";
            return isSuccess;
        }

        public List<FrontDesk> GetFrontDesks(int page, int limit, out int count)
        {
            var datas = _frontDeskDAL.GetEntities();
            count = datas.Count();
            return datas.Skip((page - 1) * limit).Take(limit).Select(m => new FrontDesk
            {
                Id = m.Id,
                EmployeeName = m.EmployeeName,
                EmployeeNo = m.EmployeeNo,
            }).ToList();
        }
    }
}
